<?php
/**
 * 管理栏目
 */

class Content_CategoryController extends Cms_AdminController {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init ();
	}
	
	public function indexAction() {
		
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		
		$tree = new Cms_Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$DB_Category = new DB_Category ();
		$result = $DB_Category->loadList ( '*', "", "listorder ASC,id ASC" );
		
		$array = array ();
		$config = Zend_Registry::get ( 'config' );
		foreach ( $result as $r ) {
			$r ['controller'] = "<a target=\"_blank\" href=\"http://www.{$config->view_conf->mainDomain}{$r['controller']}\">访问</a>";
			if ($r ['ismenu'] == 1) {
				$r ['display_icon'] = '';
			} else {
				$r ['display_icon'] = "<img title=\"不在导航显示\" src=\"{$config->view_conf->siteImgPath}/icon/gear_disable.png\">";
			}
			$r ['str_manage'] = '<a href="/content/category/add/parentid/' . $r ['id'] . '/">添加子菜单</a> | <a href="/content/category/edit/id/' . $r ['id'] . '/">修改</a> | <a href="javascript:confirmurl(\'/content/category/delete/id/' . $r ['id'] . '/\',\'' . '确认要删除 『' . $r [catname] . ' 』 吗？' . '\')">删除</a> ';
			$array [] = $r;
		}
		
		$str = "<tr>
					<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
					<td align='center'>\$id</td>
					<td >\$spacer\$catname\$display_icon</td>
					<td align='center'>\$hits</td>
					<td align='center'>\$controller</td>
					<td align='center' >\$str_manage</td>
				</tr>";
		
		$tree->init ( $array );
		$categorys = $tree->get_tree ( 0, $str );
		
		$this->view->assign ( 'categorys', $categorys );
		
		$this->render ( 'Content/category/index', null, true );
	}
	
	/**
	 * 添加栏目
	 */
	public function addAction() {
		
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$info = $this->_request->getPost ( 'info' );
			$setting = $this->_request->getPost ( 'setting' );
			$info ['setting'] = serialize ( $setting );
			
			$Model_Category = new Model_Category ();
			if ($Model_Category->add_category ( $info )) {
				$this->view->perRedirect ( '添加栏目成功！', '/content/category/' );
			} else {
				$this->view->perRedirect ( '添加栏目失败！', '/content/category/add/' );
			}
		
		} else {
			$tree = new Cms_Tree ();
			$DB_Category = new DB_Category ();
			$result = $DB_Category->loadList ( '*', "", "listorder ASC,id ASC" );
			
			$parentid = $this->_request->get ( 'parentid' );
			
			$array = array ();
			foreach ( $result as $r ) {
				$r ['selected'] = $r ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$catname</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->view->assign ( 'select_categorys', $select_categorys );
			
			$this->render ( 'Content/category/add', null, true );
		}
	}
	
	/**
	 * 编辑栏目
	 */
	public function editAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		if (isset ( $dosubmit )) {
			$id = $this->_request->getPost ( 'id' );
			
			$info = $this->_request->getPost ( 'info' );
			$setting = $this->_request->getPost ( 'setting' );
			$info ['setting'] = serialize ( $setting );
			
			$Model_Category = new Model_Category ();
			
			if ($Model_Category->edit_Category ( $id, $info )) {
				$this->view->perRedirect ( '编辑栏目成功！', '/content/category/' );
			} else {
				$this->view->perRedirect ( '编辑栏目失败！', "/content/category/edit/id/{$id}/" );
			}
		
		} else {
			$id = $this->_request->get ( 'id' );
			
			$this->view->setScriptFile ( 'formvalidator.js' );
			$this->view->setScriptFile ( 'formvalidatorregex.js' );
			
			$tree = new Cms_Tree ();
			$DB_Category = new DB_Category ();
			$result = $DB_Category->loadList ( '*', "", "listorder ASC,id ASC" );
			
			$parentid_Info = $DB_Category->loadInfoByPrimary ( $id );
			$parentid = $parentid_Info ['parentid'];
			
			$array = array ();
			foreach ( $result as $r ) {
				$r ['selected'] = $r ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$catname</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$Category_info = $DB_Category->loadInfoByPrimary ( $id );
			$Category_info ['setting'] = unserialize ( $Category_info ['setting'] );
			
			$this->view->assign ( 'Category_info', $Category_info );
			
			$this->view->assign ( 'select_categorys', $select_categorys );
			
			$this->view->assign ( 'id', $id );
			$this->render ( 'Content/category/edit', null, true );
		}
	}
	
	/**
	 * 排序
	 */
	public function listorderAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$listorders = $this->_request->getPost ( 'listorders' );
			if (is_array ( $listorders )) {
				$DB_Category = new DB_Category ();
				foreach ( $listorders as $id => $listorder ) {
					$DB_Category->updateByPrimary ( array (
							'listorder' => $listorder 
					), $id );
				}
				$this->view->perRedirect ( '操作成功！', '/content/category/' );
			}
			$this->view->perRedirect ( '操作失败！', '/content/category/' );
		}
		
		$this->view->perRedirect ( '操作失败！', '/content/category/' );
	}
	
	/**
	 * 更新栏目缓存
	 */
	public function cacheAction() {
		echo '更新栏目缓存!';
		exit ();
	}
	
	/**
	 * 删除栏目
	 * 注:递归删除栏目及子栏目
	 */
	public function deleteAction() {
		$id = $this->_request->get ( 'id' );
		
		$Model_Category = new Model_Category ();
		if ($Model_Category->delete_Category ( $id )) {
			$this->view->perRedirect ( '删除成功！', '/content/category/' );
		} else {
			$this->view->perRedirect ( '删除失败！', '/content/category/' );
		}
	}
}
?>